package com.lcc.lynx.jdbc.qw;

import com.lcc.lynx.common.utils.StrUtil;
import com.lcc.lynx.jdbc.enums.QwMethod;
import com.lcc.lynx.jdbc.enums.QwOperator;
import lombok.Data;

import java.io.Serializable;

/**
 * @author lcc
 * @date 2025/7/23 14:11
 */
@Data
public class ColumnCondition extends Column implements Serializable {

    private String as;
    private QwMethod qwMethod;
    private QwOperator qwOperator;
    private Column column;

    public ColumnCondition() {
        super();
    }


    public ColumnCondition(String columnName, QwOperator qwOperator, TableDef tableDef, Object... values) {
        super();
        this.columnName = columnName;
        this.qwOperator = qwOperator;
        Object value = values.length > 0 ? values[0] : null;
        if (value instanceof Column) {
            this.column = (Column) value;
        } else {
            this.values = values;
        }
        this.tableDef = tableDef;
    }

    public ColumnCondition(String columnName, QwMethod qwMethod, TableDef tableDef, Object... values) {
        super();
        this.columnName = columnName;
        this.qwMethod = qwMethod;
        this.qwOperator = QwOperator.EMPTY;
        Object value = values.length > 0 ? values[0] : null;
        if (value instanceof Column) {
            this.column = (Column) value;
        } else {
            this.values = values;
        }
        this.tableDef = tableDef;
    }

    public ColumnCondition(Column column, String tableAs) {
        super();
        this.columnName = column.columnName;
        this.qwOperator = QwOperator.EMPTY;
        this.tableDef = column.tableDef;
        this.tableAs = tableAs;
    }

    public String build(QueryWrapper qw) {
        String str;
        if (qwOperator.hasArg()) {
            if (column != null) {
                str = String.format(qwOperator.value(), tableColumnName(qw), column.build(qw));
            } else {
                str = String.format(qwOperator.value(), tableColumnName(qw), "#{?1}");
            }
        } else {
            str = qwOperator != QwOperator.EMPTY
                    ? tableColumnName(qw) + " " + qwOperator.value()
                    : tableColumnName(qw);
        }
        if (qwMethod != null) {
            str = qwMethod.value().replace("?0", str);
        }
        if (values != null) {
            for (int i = 1; i <= values.length; i++) {
                Object value = values[i - 1];
                if (value instanceof Number) {
                    str = str.replace("#{?" + i + "}", value.toString());
                } else {
                    int size = qw.getParams().size();
                    qw.getParams().put("p" + size, values[i - 1]);
                    str = str.replace("?" + i, "params.p" + size);
                }
            }
        }

        return str + (as == null ? "" : " as " + as);
    }

    public boolean isNotEmpty() {
        return StrUtil.isNotEmpty(values) && StrUtil.isNotEmpty(values[0]) || column != null;
    }

    public ColumnCondition as(String as) {
        this.as = as;
        return this;
    }
}
